package com.RJAI.gemini.handler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.RJAI.gemini.exception.RJException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 锐捷全局异常处理类
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
@ResponseBody
public class GlobalExceptionHandler {

    //捕获所有运行时异常
    @ExceptionHandler(Exception.class)
    public JSONObject runtimeExceptionException(RuntimeException e) throws InterruptedException {
        log.error("RuntimeException", e);
        JSONObject entries = JSONUtil.parseObj("{\"error\":{\"message\":\"当前分组上游负载已饱和，请稍后再试 (request id: 20240421071613450523387DWZKHx3N)\",\"type\":\"\",\"param\":\"\",\"code\":429}}");
        Thread.sleep( 2000 );
        return entries.getJSONObject( "error" ).set( "message", e.getMessage() );
    }
}